package com.atlin.project.service.impl;


import com.atlin.project.config.ws.MyWebSocketHandler;
import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.SysNotificationMapper;
import com.atlin.project.model.domain.SysNotification;
import com.atlin.project.model.dto.sysNotification.SysNotificationQueryRequest;
import com.atlin.project.service.SysNotificationService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author atlin
 * @description 针对表【sys_sysNotification(系统通知表)】的数据库操作Service实现
 * @createDate 2024-11-18 16:21:55
 */
@Service
@Slf4j
public class SysNotificationServiceImpl extends ServiceImpl<SysNotificationMapper, SysNotification>
        implements SysNotificationService {

    @Override
    public void notifyUser(long userId, String title, String message) {
        if (userId <= 0 || StringUtils.isBlank(message)) {
            log.error("用户id或消息不合法 userId ->{} ->{}", userId, message);
            return;
        }
        SysNotification sysSysNotification = new SysNotification();
        sysSysNotification.setUserId(userId);
        sysSysNotification.setTitle(title);
        sysSysNotification.setContent(message);
        boolean save = this.save(sysSysNotification);
        if (!save) {
            log.error("通知消息存储失败 userId ->{} ->{}", userId, message);
        }
        try {
            MyWebSocketHandler.notifyUser(userId, message);
        } catch (IOException e) {
            log.error("通知用户消息发送失败，userId ->{} ->{}", userId, message);
        }
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param sysNotificationQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<SysNotification> getQueryWrapper(SysNotificationQueryRequest sysNotificationQueryRequest) {
        QueryWrapper<SysNotification> queryWrapper = new QueryWrapper<>();
        if (sysNotificationQueryRequest == null) {
            return queryWrapper;
        }
        Long id = sysNotificationQueryRequest.getId();
        String title = sysNotificationQueryRequest.getTitle();
        String content = sysNotificationQueryRequest.getContent();
        Long userId = sysNotificationQueryRequest.getUserId();
        String sortField = sysNotificationQueryRequest.getSortField();
        String sortOrder = sysNotificationQueryRequest.getSortOrder();

        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        return queryWrapper;
    }
}




